﻿using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillEffective_Totem : SkillEffective
    {
        protected override bool effectiveOne => true;

        protected override ISerializeCustomPOD OnExecute(SkillEffectiveData data, Unit target, SkillEffectiveContext context, int effectiveIndex, out SkillEffectiveResponse startResp)
        {
            startResp = null;
            if (data.effectiveConfig.customConfig is SkillEffective_Totem_ConfigData rConfig)
            {
                var rSummoner = context.skill.executer;
                var rPlayCtrl = rSummoner.battle.playCtrl;

                var rPos = context.selectParam.pos;

                // 创建单位
                var rTotem = UnitFactory.CreateBattleTotem(rPlayCtrl.battle, context.skill.executer, rPos, rConfig.duration);

                rPlayCtrl.unitCtrl.AddUnit(rTotem);
                rSummoner.summon.Add(rTotem);

                var rTotemComp = rTotem.GetComponent<TotemComponent>(ETComponentType.TotemComponent);
                rTotemComp.Initialize(rConfig, context);

                var rResp = ClassPool.Get<TotemResponse>();
                rResp.SetUnit(rTotem, rSummoner);
                rResp.effectName = rConfig.effectName;
                rTotemComp.TryActive(SkillEffective_Totem_ConfigData.EActiveType.Now, rResp, out var rEffectiveResp);

                startResp = rEffectiveResp; // 放在start中用于创建单位
                return rResp;
            }
            return null;
        }

#if UNITY_EDITOR
        public override string ToString(SkillCustomConfig config)
        {
            var rConfig = config as SkillEffective_Totem_ConfigData;
            return $"光环图腾";
        }
#endif
    }
}